package com.phpxiu.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.ExchangeContent;
import com.phpxiu.app.view.Exchange;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/6/4.
 */
public class ExchangeListAdapter extends BaseAdapter<ExchangeContent> implements View.OnClickListener {
    private WeakReference<Exchange>weakExchange;
    private String unit;
    public ExchangeListAdapter(Context context, List<ExchangeContent> data) {
        super(context, data);
        unit=context.getString(R.string.ticket_unit);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ExchangeContent content=data.get(position);
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.exchange_list_item_view,null);
        }
        TextView costView=(TextView)convertView.findViewById(R.id.cost_value);
        costView.setText(content.getCoin());
        Button valueView=(Button)convertView.findViewById(R.id.value_btn);
        valueView.setTag(content);
        valueView.setOnClickListener(this);
        valueView.setText(content.getVote() +unit);
        convertView.findViewById(R.id.divider_view).setVisibility(View.VISIBLE);
        if(data.size()>0){
            if(position==data.size()-1){
                convertView.findViewById(R.id.divider_view).setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if(v.getTag()!=null&&v.getTag() instanceof ExchangeContent){
            ExchangeContent content=(ExchangeContent)v.getTag();
            if(weakExchange!=null&&weakExchange.get()!=null){
                weakExchange.get().onItemClick(content);
            }
        }
    }

    public void addClickCallBack(Exchange exchange){
        if(exchange==null){
            if(weakExchange!=null){
                weakExchange.clear();
                weakExchange=null;
            }
        }else{
            weakExchange=new WeakReference<Exchange>(exchange);
        }
    }
}
